# Necessary to run on Site5
set :use_sudo, false
set :group_writable, false

# Less releases, less space wasted
set :keep_releases, 2

# The mandatory stuff
set :application, "pilarvillegasnd"
set :user, "drpilarv"

set :repository,  "https://cms-on-rails.googlecode.com/svn/branches/pv"

# SCM information
set :scm_username, "spejic"
set :scm_password, Proc.new { CLI.password_prompt "SVN Password: "}

# This is related to site5 too.
set :deploy_to, "/home/#{user}/apps/#{application}"

role :app, "www.pilarvillegasnd.com"
role :web, "www.pilarvillegasnd.com"
role :db,  "www.pilarvillegasnd.com", :primary => true


# In the deploy namespace we override some default tasks and we define
# the site5 namespace.
namespace :deploy do
  desc <<-DESC
    Deploys and starts a `cold� application. This is useful if you have not \
    deployed your application before, or if your application is (for some \
    other reason) not currently running. It will deploy the code, run any \
    pending migrations, and then instead of invoking `deploy:restart�, it will \
    invoke `deploy:start� to fire up the application servers.
  DESC
  # NOTE: we kill public_html so be sure to have a backup or be ok with this application
  # being the default app for the domain.
  task :cold do
    update_db
    site5::link_public_html
    site5::kill_dispatch_fcgi
  end
  
  task :update_db do
    desc <<-DESC
      Update the db schema if needed
    DESC
    run "cd /home/#{user}"
    run "cp database.yml #{deploy_to}/current/config"
    run "cd  #{deploy_to}/current"
    run "rake db:migrate"
    run "rake tmp:assets:clear"
  end
  
  desc <<-DESC
    Site5 version of restart task.
  DESC
  task :restart do
    site5::kill_dispatch_fcgi
    site5::copy_database_yml
    site5::clear_assets
  end
    
  namespace :site5 do
    desc <<-DESC
      Links public_html to current_release/public
    DESC
    task :link_public_html do
      run "cd /home/#{user}; rm -rf public_html; ln -s #{current_path}/public ./public_html;"
      run "cd #{current_path}; rake tmp:assets:clear"
    end
    
    desc <<-DESC
      Kills Ruby instances on Site5
    DESC
    task :kill_dispatch_fcgi do
      run "skill -u #{user} -c ruby"
      run "chmod 755 -R #{deploy_to}/current/public/"
      run "chmod 755 #{deploy_to}/current/public/dispatch.*"
    end

    desc <<-DESC
      Copy over database.yml file since it's not in repo
    DESC
    task :copy_database_yml do
      run "cd /home/#{user}"
      run "cp database.yml #{deploy_to}/current/config"
    end

    desc <<-DESC
      Clear all cached pages and JS/CSS
    DESC
    task :clear_assets do
      run "cd  #{deploy_to}/current"
      run "rm -rf log"
      run "mkdir log"
      run "rake tmp:assets:clear"
    end
  end
end